import os.path

filename = 'student.txt'


def main():
    while (True):
        menum()
        choice = int(input("请选择"))
        if choice in [1, 2, 3, 4, 5, 6, 7, 0]:
            if choice == 0:
                answer = input('您确定呀退出系统吗?')
                if answer == 'Y' or answer == 'y':
                    print("感谢您的使用")
                    break
                else:
                    continue
            elif choice == 1:
                insert()
            elif choice == 2:
                serch()
            elif choice == 3:
                delete()
            elif choice == 4:
                modify()
            elif choice == 5:
                sort()
            elif choice == 6:
                total()
            elif choice == 7:
                show()


def menum():
    print("=====================学生信息管理系统=========================")
    print("------------------功能菜单--------------------")
    print("1.录入学生信息")
    print("2.查找学生信息")
    print("3、删除学生信息")
    print("4.修改学生信息")
    print("5.排序")
    print("6.统计学生总人数")
    print("7.显示所有学生信息")
    print("0.退出")


def insert():
    print("--------------开始录入学生信息---------------")
    student_list = []
    while (True):
        id = int(input("请输入ID(如1001)"))
        if not id:
            break
        name = input("请输入姓名")
        if not name:
            break
        try:
            englist = int(input("请输入英语成绩"))
            python = int(input("请输入python成绩"))
            java = int(input("请输入java成绩"))
        except:
            print("输入无效,不是整数类型,请重新输入")
            continue
        student = {'id': id, 'name': name, 'englist': englist, 'python': python, 'java': java}
        student_list.append(student)
        answer = input("是否继续添加?y/n\n")
        if answer == 'y':
            continue
        elif answer == 'n':
            break
        else:
            print("输入{0}无效".format(answer))
    # 存储数据到文件中
    save(student_list)
    print("学生信息录入完毕")
    pass


def save(student_list):
    try:
        stu_txt = open(filename, 'a', encoding='utf-8')
    except:
        stu_txt = open(filename, 'w', encoding='utf-8')
    for item in student_list:
        stu_txt.write(str(item)+'\n')
    stu_txt.close()

def serch():
    print("--------------开始查找学生信息---------------")
    pass


def delete():
    print("--------------开始删除学生信息---------------")
    student_oid=[]
    while True:
        student_id =input("请输入需要删除的学生id:")
        if student_id !='':
            if os.path.exists(filename):
                with open(filename,'r',encoding='utf-8') as file:
                    student_oid.append(file.readline())

            flag=False
            print(student_oid)
            if student_oid:
                # with open(filename,'w','utf-8') as wfile:

                stu_txt = open(filename, 'w', encoding='utf-8')
                for item in student_oid:
                    d=dict(item)
                    if d['id'] != student_id:
                        # wfile.write(item+'\n')
                        stu_txt.write(item)
                    else:
                        flag =True
                        continue
                if file:
                    print(f"id为{student_id}的学生信息已经被删除")
                else:
                    print(f"没有找到ID为{student_id}的学生")
            else:
                print("当前无学生信息")
                break
            show()
            answer=input("是否继续删除y/n\n")
            if answer=='y':
                continue
            else:
                stu_txt.close()
                break




def modify():
    print("--------------开始修改学生信息---------------")
    pass


def sort():
    print("--------------开始排序---------------")
    pass


def total():
    print("--------------开始统计学生总人数---------------")
    pass


def show():
    print("--------------开始退出---------------")
    pass


main()
